package com.franzliszt.professionalcalcuator;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

@SuppressLint("AppCompatCustomView")
public class BtnCircle extends TextView {
    //圆的画笔
    private Paint CirclePaint = new Paint();
    //圆的颜色
    private int CircleColor;
    private int SelectCircle;
    private boolean isSelect = false;

    public BtnCircle(Context context) {
        super(context);
    }

    public BtnCircle(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        InitAttrs(context,attrs);
        InitVar();
    }

    public BtnCircle(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    private void InitAttrs(Context context,AttributeSet attr){
        TypedArray array = context.getTheme().obtainStyledAttributes(attr,R.styleable.SetCircle,0,0);
        CircleColor = array.getColor(R.styleable.SetCircle_CircleColor,0xFFFFFFFF);
        SelectCircle = array.getColor(R.styleable.SetCircle_SelectCircle,0xFFFFFFFF);
    }
    private void InitVar(){
        //设置颜色
        CirclePaint.setColor( CircleColor );
        //设置填充方式
        CirclePaint.setStyle( Paint.Style.FILL );
        //设置抗锯齿
        CirclePaint.setAntiAlias( true );
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (isSelect){
            CirclePaint.setColor( SelectCircle );
        }else {
            CirclePaint.setColor( CircleColor );
        }
        //设置填充方式
        CirclePaint.setStyle( Paint.Style.FILL );
        //设置抗锯齿
        CirclePaint.setAntiAlias( true );

        RectF rectF = new RectF();
        //设置半径,比较长宽,取最大值
        int radius = getMeasuredWidth() > getMeasuredHeight() ? getMeasuredWidth() : getMeasuredHeight();
        //设置Padding 不一致,绘制出的是椭圆;一致的是圆形
        rectF.set(getPaddingLeft(),getPaddingTop(),radius-getPaddingRight(),radius-getPaddingBottom());
        //绘制圆弧
        canvas.drawArc(rectF,0,360,false,CirclePaint);
        //最后调用super方法,解决文本被所绘制的圆圈背景锁覆盖的问题
        super.onDraw(canvas);
    }
    public void IsSelect(boolean isSelect){
        this.isSelect = isSelect;
        invalidate();
    }
}
